﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Castle.DynamicProxy.Generators.Emitters;
using Castle.Windsor;
using M2B.GestionEntretiens.Contracts;
using M2B.GestionEntretiens.Helpers.JSON;
using M2B.GestionEntretiens.Models;
using M2B.GestionEntretiens.POCOS;
using Newtonsoft.Json;

namespace M2B.GestionEntretiens.Controllers
{
    public class EntretiensController : Controller
    {

        /// <summary>
        /// Statuts des entretiens
        /// </summary>
        public List<Referentiel> StatutsEntretiens
        {
            get
            {
                var refrentielsModel = Session["RefrentielsModel"] as RefrentielsModel;
                if (refrentielsModel != null)
                {
                    return refrentielsModel.Referentiels.Where(el => el.MotCle == "STATUT_ENTRETIEN").ToList();
                }
                return new List<Referentiel>();
            }
        }

        /// <summary>
        /// Langue des entretiens
        /// </summary>
        public List<Referentiel> LangueEntretiens
        {
            get
            {
                var refrentielsModel = Session["RefrentielsModel"] as RefrentielsModel;
                if (refrentielsModel != null)
                {
                    return refrentielsModel.Referentiels.Where(el => el.MotCle == "LANGUE_CANDIDAT").ToList();
                }
                return new List<Referentiel>();
            }
        }
        /// <summary>
        /// Langue des entretiens
        /// </summary>
        public List<Referentiel> TypeTests
        {
            get
            {
                var refrentielsModel = Session["RefrentielsModel"] as RefrentielsModel;
                if (refrentielsModel != null)
                {
                    return refrentielsModel.Referentiels.Where(el => el.MotCle == "TYPE_TEST").ToList();
                }
                return new List<Referentiel>();
            }
        }

        /// <summary>
        /// Entretiens Model
        /// </summary>
        public EntretiensModel EntretiensModel
        {
            get
            {

                var entretiensModel = Session["EntretiensModel"] as EntretiensModel;
                if (entretiensModel == null)
                {
                    var container = Session["Container"] as IWindsorContainer;
                    if (container != null) entretiensModel = new EntretiensModel(container.Resolve<IEntretiensService>());
                    Session["EntretiensModel"] = entretiensModel;
                }
                return entretiensModel;

            }
            private set { Session["EntretiensModel"] = value; }
        }

        //
        // GET: /Entretiens/

        public ActionResult Index()
        {
            ViewData["StatutsEntretiens"] = StatutsEntretiens;
            ViewData["LangueEntretiens"] = LangueEntretiens;
            return View();
        }

        /// <summary>
        /// Chargement de la liste des entretiens
        /// </summary>
        /// <returns>Entretiens</returns>
        public ActionResult LoadEntretiens()
        {
            string strEntretiens = JsonConvert.SerializeObject(EntretiensModel.Entretiens,
               new EFNavigationPropertyConverter());
            return Json(strEntretiens, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Supprimer un entretien
        /// </summary>
        /// <param name="entretienId">Entretien Id</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeleteEntretien(int entretienId)
        {
            try
            {
                EntretiensModel.DeleteEntretien(entretienId);
                return new EmptyResult();
            }
            catch
            {
                return View("Index");
            }
        }

        /// <summary>
        /// Ajouter un Entretien
        /// </summary>
        /// <returns></returns>
        public ActionResult AddEntretien()
        {
            ViewData["StatutsEntretiens"] = StatutsEntretiens;
            ViewData["LangueEntretiens"] = LangueEntretiens;
            ViewData["TypeTests"] = TypeTests;

            return PartialView(new Entretien() { Statut = 1, Langue = "1", Candidat = new Candidat(), Test = new Test() });
        }
        /// <summary>
        /// Ajouter un nouveau Entretien
        /// </summary>
        /// <param name="entretien">Entretien</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CreateEntretien(Entretien entretien)
        {
            try
            {
                EntretiensModel.CreateEntretien(entretien);
                return RedirectToAction("Index");
            }
            catch
            {
                return View("Index");
            }
        }


        [HttpGet]
        public ActionResult SearchCandidat(string textSearch)
        {

            return RedirectToAction("SearchCandidats", "Candidats", new { textSearch = textSearch });
        }

        [HttpGet]
        public ActionResult SearchTest(string textSearch, string typeTest)
        {

            return RedirectToAction("SearchTest", "Tests", new { textSearch = textSearch, typeTest = typeTest });
        }
        /// <summary>
        /// Modification d'un Entretien
        /// </summary>
        /// <param name="entretienId">Id</param>
        /// <returns></returns>
        public ActionResult EditEntretien(int entretienId)
        {

            ViewData["StatutsEntretiens"] = StatutsEntretiens;
            ViewData["LangueEntretiens"] = LangueEntretiens;
            ViewData["TypeTests"] = TypeTests;

            var entretien = EntretiensModel.GetEntretien(entretienId);
            return PartialView(entretien);
        }
        [HttpPost]
        public ActionResult Edit(Entretien entretien)
        {
            try
            {
                EntretiensModel.Update(entretien);
                return RedirectToAction("Index");
            }
            catch
            {
                return View("Index");
            }
        }
    }
}
